package com.wjb.xietong.component;

import java.io.File;
import java.lang.Thread.UncaughtExceptionHandler;

import com.wjb.xietong.W3.HttpUtil;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.DisplayMetrics;

public class AppGlobal extends Application {
	private static AppGlobal application;

	private ActManager actManager;

	public static final String URL = "http://www.51xunjian.com:8083/AppHandler.ashx";
	public static final String androidFileName_ = Environment
			.getExternalStorageDirectory().getAbsolutePath() + "/.e5";// "/data/data/com.zoeice.e5/";

	/**
	 * 屏幕参数
	 */
	private DisplayMetrics displayMetrics = null;
	private int titleBarHeight = 0;
	private int tabBarHeight = 0;

	private String JSESSIONID = null;
	private String imei = null;
	private String appPath = null;

	private boolean isHomeShownNotice = false;

	private String localAppVersion = null;// 本地App版本

	private String clientId = null;

	private PendingIntent restartIntent;
	private String packgeName;
	private MyUncaughtExceptionHandler uncaughtExceptionHandler;  

	public static AppGlobal getInstance() {
		return application;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		application = this;
		File f = new File(androidFileName_);
		if (!f.isFile()) {
			f.mkdir();
		}
		HttpUtil.init(this);// 初始化网络请求队列
		
		packgeName = getPackageName();
		
		cauchException();
	}

	/**
	 * 程序崩溃时触发此线程
	 */
	private void cauchException() {
		Intent intent = new Intent();
		// 参数1：包名，参数2：程序入口的activity
		intent.setClassName(packgeName, packgeName + ".OTASplash");
		restartIntent = PendingIntent.getActivity(getApplicationContext(), -1,
				intent, Intent.FLAG_ACTIVITY_NEW_TASK);

		// 程序崩溃时触发线程
		uncaughtExceptionHandler = new MyUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
	}

	// 创建服务用于捕获崩溃异常
	private class MyUncaughtExceptionHandler implements
			UncaughtExceptionHandler {
		@Override
		public void uncaughtException(Thread thread, Throwable ex) {
			// 1秒钟后重启应用
			AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
			mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
					restartIntent);

			// 关闭当前应用
			finishAllActivity();
			finishProgram();
			System.out.println("uncaughtException");
		}
	};
	
	// activity管理：结束所有activity  
    public void finishAllActivity() {  
       getActManager().popAllActivity();
    }  
  
    // 结束线程,一般与finishAllActivity()一起使用  
    // 例如: finishAllActivity;finishProgram();  
    public void finishProgram() {  
        android.os.Process.killProcess(android.os.Process.myPid());  
    }  

	/**
	 * 获取Activity管理器
	 * 
	 * @return
	 */
	public ActManager getActManager() {
		return actManager;
	}

	public void setActManager(ActManager actManager) {
		this.actManager = actManager;
	}

	/**
	 * 获取BaseOTA context
	 * 
	 * @return
	 */
	public Context getContext() {
		return getActManager().currentActivity();
	}

	/**
	 * 获取个推clientId
	 */
	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	/**
	 * 获取当前App版本
	 */
	public String getLocalAppVersion() {
		return localAppVersion;
	}

	public void setLocalAppVersion(String localAppVersion) {
		this.localAppVersion = localAppVersion;
	}

	/**
	 * 获取IMEI号
	 */
	public String getIMEIDeviceID() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	/**
	 * 获取sessionID
	 */
	public String getJSESSIONID() {
		return JSESSIONID;
	}

	public void setJSESSIONID(String jSESSIONID) {
		JSESSIONID = jSESSIONID;
	}

	/**
	 * 获取当前程序路径
	 */
	public String getAppPath() {
		return appPath;
	}

	public void setAppPath(String appPath) {
		this.appPath = appPath;
	}

	/**
	 * 检查SDcard是否存在
	 * 
	 * @return
	 */
	public boolean hasSdcard() {
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

	public void setDisplayMetrics(DisplayMetrics displayMetrics) {
		this.displayMetrics = displayMetrics;
		titleBarHeight = (int) (44 * displayMetrics.density);
		tabBarHeight = (int) (55 * displayMetrics.density);
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @return 屏幕宽度，单位px
	 */
	public int getScreenWidth() {
		return displayMetrics.widthPixels;
	}

	/**
	 * 获取屏幕高度
	 * 
	 * @return 屏幕高度，单位px
	 */
	public int getScreenHeight() {
		return displayMetrics.heightPixels;
	}

	/**
	 * 获取屏幕密度
	 * 
	 * @return 屏幕密度
	 */
	public float getScreenDensity() {
		return displayMetrics.density;
	}

	/**
	 * 获取标题栏高度
	 * 
	 * @return 标题栏高度，单位px
	 */
	public int getTitleBarHeight() {
		return titleBarHeight;
	}

	/**
	 * 获取TabBar高度
	 * 
	 * @return TabBar高度，单位px
	 */
	public int getTabBarHeight() {
		return tabBarHeight;
	}

	public boolean isHomeShownNotice() {
		return true;// isHomeShownNotice;
	}

	public void setHomeShownNotice(boolean isHomeShownNotice) {
		this.isHomeShownNotice = isHomeShownNotice;
	}

}
